<template>
  <div class="modern-page error-page">
    <div class="error-content modern-card modern-slide-in">
      <div class="error-icon modern-float">
        <component :is="'WifiOffIcon'" />
      </div>
      <div class="error-code modern-text-gradient error-code-network">无网络</div>
      <div class="error-title">网络连接失败</div>
      <div class="error-description">
        请检查您的网络连接，然后重试
      </div>
      <div class="network-status">
        <div class="status-item" :class="{ 'status-error': !networkStatus.internet }">
          <component :is="networkStatus.internet ? 'CheckCircleIcon' : 'XCircleIcon'" />
          <span>互联网连接</span>
          <span class="status-text">{{ networkStatus.internet ? '正常' : '异常' }}</span>
        </div>
        <div class="status-item" :class="{ 'status-error': !networkStatus.server }">
          <component :is="networkStatus.server ? 'CheckCircleIcon' : 'XCircleIcon'" />
          <span>服务器连接</span>
          <span class="status-text">{{ networkStatus.server ? '正常' : '异常' }}</span>
        </div>
      </div>
      <div class="error-actions">
        <t-button 
          theme="primary" 
          size="large"
          class="modern-btn-enhance"
          @click="checkNetwork"
          :loading="checking"
        >
          <template #icon>
            <component :is="'RefreshIcon'" />
          </template>
          检查网络
        </t-button>
        <t-button 
          variant="outline" 
          size="large"
          @click="retry"
        >
          <template #icon>
            <component :is="'PlayIcon'" />
          </template>
          重新加载
        </t-button>
        <t-button 
          variant="text" 
          size="large"
          @click="offlineMode"
        >
          <template #icon>
            <component :is="'DownloadIcon'" />
          </template>
          离线模式
        </t-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'

const checking = ref(false)
const networkStatus = ref({
  internet: false,
  server: false
})

const checkNetwork = async () => {
  checking.value = true
  
  try {
    // 检查互联网连接
    const response = await fetch('https://www.baidu.com', { 
      mode: 'no-cors',
      timeout: 5000 
    })
    networkStatus.value.internet = true
  } catch (error) {
    networkStatus.value.internet = false
  }
  
  try {
    // 检查服务器连接（这里应该替换为实际的API地址）
    const response = await fetch('/api/health', { timeout: 5000 })
    networkStatus.value.server = response.ok
  } catch (error) {
    networkStatus.value.server = false
  }
  
  checking.value = false
  
  if (networkStatus.value.internet && networkStatus.value.server) {
    MessagePlugin.success('网络连接正常')
    setTimeout(() => {
      window.location.reload()
    }, 1000)
  } else {
    MessagePlugin.warning('网络连接仍有问题')
  }
}

const retry = () => {
  window.location.reload()
}

const offlineMode = () => {
  MessagePlugin.info('离线模式开发中...')
}

onMounted(() => {
  checkNetwork()
})
</script>

<style lang="scss" scoped>
.error-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
}

.error-content {
  text-align: center;
  padding: 3rem 2rem;
  max-width: 500px;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.error-icon {
  font-size: 4rem;
  color: #64748b;
  margin-bottom: 1.5rem;
  animation: bounce 2s infinite;
}

.error-code-network {
  background: linear-gradient(135deg, #64748b, #475569);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  font-size: 4rem;
  font-weight: 800;
  line-height: 1;
  margin-bottom: 1.25rem;
  letter-spacing: -0.02em;
}

.error-title {
  font-size: 1.5rem;
  font-weight: 600;
  color: #1f2937;
  margin-bottom: 0.75rem;
}

.error-description {
  font-size: 1rem;
  color: #6b7280;
  margin-bottom: 1.5rem;
  line-height: 1.6;
}

.network-status {
  background: rgba(241, 245, 249, 0.8);
  border-radius: 12px;
  padding: 1.5rem;
  margin-bottom: 2rem;
  border: 1px solid rgba(148, 163, 184, 0.2);
}

.status-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
  color: #10b981;
  font-size: 0.9rem;
  
  &:last-child {
    margin-bottom: 0;
  }
  
  &.status-error {
    color: #ef4444;
  }
  
  svg {
    font-size: 1.25rem;
    flex-shrink: 0;
  }
  
  .status-text {
    margin-left: auto;
    font-weight: 600;
  }
}

.error-actions {
  display: flex;
  gap: 1rem;
  justify-content: center;
  flex-wrap: wrap;
}

:deep(.t-button) {
  border-radius: 12px;
  height: 3rem;
  padding: 0 1.5rem;
  font-weight: 600;
  transition: all 0.3s ease;
}

:deep(.t-button--theme-primary) {
  background: linear-gradient(135deg, #10b981, #059669);
  border: none;
}

:deep(.t-button--theme-primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(16, 185, 129, 0.4);
}

:deep(.t-button--variant-outline) {
  border: 2px solid rgba(0, 0, 0, 0.1);
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(5px);
}

:deep(.t-button--variant-outline:hover) {
  background: rgba(255, 255, 255, 0.9);
  transform: translateY(-2px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

:deep(.t-button--variant-text) {
  background: transparent;
  color: #6b7280;
}

:deep(.t-button--variant-text:hover) {
  background: rgba(107, 114, 128, 0.1);
  color: #374151;
}

@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-10px);
  }
}

// 响应式设计
@media (max-width: 640px) {
  .error-content {
    padding: 2rem 1.5rem;
  }
  
  .error-code-network {
    font-size: 3rem;
  }
  
  .network-status {
    padding: 1rem;
  }
  
  .error-actions {
    flex-direction: column;
    align-items: center;
    
    :deep(.t-button) {
      min-width: 200px;
    }
  }
}
</style> 